#include "Type_Define.h"
#include "IO_LINK_Global_Param.h"
#include "DL_EH.h"
#include "DL_Message_Handler.h"


// 测试git 819 

EH_STATE_TYPE EH_STATE[PORT_NUM];
EH_EventInfo_Typedef EH_EventInfo[PORT_NUM][6];
uint8_t EH_EventNum[PORT_NUM];   // Event总数 
uint8_t EH_EventAddress[PORT_NUM][18];//保存读取在EventMemrory地址
uint8_t EH_EventAddress_Point[PORT_NUM]; //读取EventAddress的指针
uint8_t EH_EventMemory[PORT_NUM][18];//用于保存读取的错误信息Slot
//Table A.16 Type1的EventCode对照表
const uint16_t EventCode_Type1[]={0xFF80, 0xFF80,0x6320,0xFF80,0xFF10};
void Init_EH(void){
    for(uint8_t i=0;i<PORT_NUM;i++){
       EH_STATE[i]=EH_Inactive_0;
    }
}

/*********************************************************/

void DL_EH_T2(uint8_t Port){
    //构造OD.Req
    *MC_Struct[Port].MC=0xC0;
    Event_OD(Port);
        
}

 

//收到EventStatus后解析，判断Error有几条()，每条地址
void DL_EH_T2_1(uint8_t Port){
   uint8_t StatusCode=*MC_Struct[Port].OD;
   EH_EventNum[Port]=0;
   EH_EventAddress_Point[Port]=0;
   if((StatusCode&0x80)==0x80){ //EventDetails==1 ,StatusCode Type2
       uint8_t temp1,temp2,Address;
       Address=0; //EventMemory的Address
       for(uint8_t i=0;i<6;i++){
            temp1=StatusCode&(1<<i);
            temp2=temp1>>i; 
            if(temp2==0x01){ //i位置有Event
                EH_EventNum[Port]++;
                EH_EventAddress[Port][Address]=0x01+i*3;
                EH_EventAddress[Port][Address]=0x02+i*3;
                EH_EventAddress[Port][Address]=0x03+i*3;
            }
       }

   }else{    //EventDetails==0 ,StatusCode Type1
       // 此时无需读取EventMemory，根据Table A.16直接构造 EventCodes
       for(uint8_t i=0;i<5;i++){
				   
           uint8_t temp1=StatusCode&(1<<i);
           uint8_t temp2=temp1>>i; 
            if(temp2==0x01){ //i位置有Event
                EH_EventNum[Port]++;
                //Type1 直接构造EventMemory
                EH_EventInfo[Port][i].EventCode=EventCode_Type1[i];
                EH_EventInfo[Port][i].Instance=4;  //Application
                EH_EventInfo[Port][i].Mode=1; //Single shot
                EH_EventInfo[Port][i].Type=1;  //Notification

            }
       }
       //读取Event结束
       Event_Events_completed(Port);
   }
}

void DL_EH_T3(uint8_t Port){
   //构造OD.Req
   *MC_Struct[Port].MC=0xC0+EH_EventAddress[Port][EH_EventAddress_Point[Port]];
   Event_OD(Port);
}

//提取EventCode
void DL_EH_T3_1(uint8_t Port){
   EH_EventMemory[Port][EH_EventAddress_Point[Port]]=*MC_Struct[Port].OD;
    
     //更新EH_EventAddress_Point，以便下次使用
   EH_EventAddress_Point[Port]++;
   if(EH_EventAddress_Point[Port]>=EH_EventNum[Port]){ //说明已经读取EventCOde完成
        Event_Events_completed(Port);
   }
   
}

void DL_EH_T4(uint8_t Port){
    Event_Events_reported(Port);
}

void DL_EH_T8(uint8_t Port){
	 //构造OD.Req
   *MC_Struct[Port].MC=0x40;
	  //发送任意内容均可
	  Event_OD(Port);
}


/*************输入事件************************/

void Event_EH_Conf_ACTIVE(uint8_t Port){
    if(EH_STATE[Port]==EH_Inactive_0){
        EH_STATE[Port]=EH_Idle_1;
      //  DL_EH_T1(Port);
    }
}

void Event_EH_Conf_INACTIVE(uint8_t Port){
    
        EH_STATE[Port]=EH_Inactive_0;
       
   
}


void Event_EventTrig(uint8_t Port){
    if(EH_STATE[Port]==EH_Idle_1){
        EH_STATE[Port]=subEH_Idle_1; //进入自定义子状态，由Event_OD_Return返回
        DL_EH_T2(Port);
    }

    if(EH_STATE[Port]==EH_ReadEvent_2){
        EH_STATE[Port]=subEH_ReadEvent_2; //进入自定义子状态，由Event_OD_Return返回
        DL_EH_T3(Port);
        return;
    }
    if(EH_STATE[Port]==EH_EventConfirmation_4){
       EH_STATE[Port]=EH_EventConfirmation_4;  
       DL_EH_T8(Port);
       return;
    }
    
}

void Event_DL_EventConf(uint8_t Port){
    if(EH_STATE[Port]==EH_Idle_1){
        EH_STATE[Port]=EH_EventConfirmation_4;
       // DL_EH_T7(Port);
    }
}


void Event_Events_completed(uint8_t Port){
    if(EH_STATE[Port]==EH_ReadEvent_2){
        EH_STATE[Port]=EH_SignalEvent_3;
        DL_EH_T4(Port);
    }
}

void Event_Events_reported(uint8_t Port){
    if(EH_STATE[Port]==EH_SignalEvent_3){
        EH_STATE[Port]=EH_Idle_1;
        //DL_EH_T5(Port);
    }
}

